# -*- coding: utf-8 -*-
# !/usr/bin/env python

import time
import random
import os

PHONE_LENTH = 1919
PHONE_WIDTH = 1066


DEBUG = True
def printD(str):
    if DEBUG:
        print str
    pass

class YYS:
    def __init__(self,device,adbdev):
        self.device=device
        self.adbdev = adbdev
        pass

    def startGame(self):
        try:
            self.device.screen.on()
            #printD("Press Home")
            self.device.press.home()
            self.device(text="阴阳师").long_click()
            #time.sleep(3)
            self.device(text="正在连接服务器").wait.gone()
            self.device.long_click(943, 917)
            self.device(text="切换").wait.exists()
            self.device.long_click(943,917)
            self.device(text="切换").wait.gone()
            self.device.long_click(2116,362)
            self.device.swipe(280,200,1200,800,10)
            self.device.drag(1200,800,280,200,10)
            self.device.drag(1200, 800, 280, 200, 10)
            #time.sleep(3)
            self.device.long_click(1243, 1211)
            self.device.long_click(1243, 1211)
            #time.sleep(3)
            self.device.long_click(1243, 1211)
            self.device.long_click(1243, 1211)
            return "OK"
        except Exception,e:
            print e
            return "Fail"

    def CreatNewAcount(self):
        try:
            self.device.press(966,886)
        except:
            print "进入游戏失败"
        name = self.getRandomName()
        #self.writestr(name)
        return name
    def LogInGame(self):
        try:
            self.device.press(PHONE_LENTH/2, PHONE_WIDTH/6)
            self.device.press(PHONE_LENTH/2, PHONE_WIDTH/6)
        except:
            print "进入游戏失败"

    # 在ascill下 48-57是数字，65-90 是大写字母，97-122是小写字母
    # 在android系统下 7-16 是数，29-54 是大写字母
    def write_str(self,str):
        for i in range(0, len(str)):
            if ord(str[i]) in range(48, 57):
                num = ord(str[i]) - 48 + 7
                cmd_line = "shell input keyevent %d" % num
            if str[i].islower():
                num = ord(str[i]) - 97 + 29
                cmd_line = "shell input keyevent %d" % num
            if str[i].isupper():
                num = ord(str[i]) - 65 + 29
                cmd_line = "shell input keyevent %d" % num
            if str[i].isspace():
                cmd_line = "shell input keyevent 62"
            if str[i] == ",":
                cmd_line = "shell input keyevent 55"
            if str[i] == ".":
                cmd_line = "shell input keyevent 56"
            self.adbdev.cmd(cmd_line)
            time.sleep(0.1)
        pass
    def getRandomName(self):
        low = random.randint(65, 90)
        up = random.randint(65, 90)
        num = random.randint(48, 57)
        return "阴阳师欧阳%c%c%c" %(up,low,num)



